package com.lc.app.androidfirst.userdefine.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.lc.app.androidfirst.MainActivity;
import com.lc.app.androidfirst.R;

import java.util.logging.Logger;

/**
 * Created by LC on 12/18/2015.
 */
public class DrawView2 extends View {
    public DrawView2(Context context) {
        super(context);
    }

    private int buttonType;

    public DrawView2(Context context,int buttonType) {
        super(context);
        this.buttonType = buttonType;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.drawRect(x ,y ,x add length, y add length)

        // draw rectangle with shadow
        if(buttonType == 1){
            Paint paint = new Paint();
            Shader shader = new LinearGradient(0,0,50,50,Color.RED,Color.GREEN, Shader.TileMode.MIRROR);
            paint.setShader(shader);
            canvas.drawRect(10, 70, 100, 150, paint);

            shader = new RadialGradient(160,110,50,Color.RED,Color.GREEN, Shader.TileMode.MIRROR);
            paint.setShader(shader);
            canvas.drawRect(117, 70, 205, 150, paint);

            shader = new SweepGradient(265,110,new int[]{Color.RED,Color.GREEN,Color.BLUE},null);
            paint.setShader(shader);
            canvas.drawRect(220, 70, 310, 150, paint);

            Paint paint2 = new Paint();
            paint2.setColor(Color.RED);
            paint2.setShadowLayer(2, 3, 3, Color.rgb(180, 180, 180));
            Shader shader2 = new LinearGradient(0,0,50,50,Color.RED,Color.GREEN, Shader.TileMode.MIRROR);
            paint2.setShader(shader2);
            canvas.drawRect(10, 200, 100, 280, paint2);

            shader2 = new RadialGradient(160,110,50,Color.RED,Color.GREEN, Shader.TileMode.MIRROR);
            paint2.setShader(shader2);
            canvas.drawRect(117, 200, 205, 280, paint2);

            shader2 = new SweepGradient(265,110,new int[]{Color.RED,Color.GREEN,Color.BLUE},null);
            paint2.setShader(shader2);
            canvas.drawRect(220,200,310,280,paint2);

            Paint paint1 = new Paint();
            paint1.setColor(Color.RED);
            paint1.setShadowLayer(2, 3, 3, Color.rgb(180, 180, 180));
            canvas.drawRect(140,300,240,350,paint1);
        }

        if(buttonType == 2){
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(2);
            paint.setStyle(Paint.Style.STROKE);

            RectF rectf = new RectF(10,20,100,110);
            canvas.drawArc(rectf, 0, 30, true, paint);
            RectF rectf2 = new RectF(50,20,190,110);
            canvas.drawArc(rectf2, 0, 90, false, paint);

            canvas.drawCircle(100, 150, 30, paint);

            canvas.drawLine(10, 200, 40, 200, paint);
            canvas.drawLine(10, 205, 20, 205, paint);
            canvas.drawLines(new float[]{10, 220, 170, 220, 10, 220, 10, 340, 10, 340, 170, 220}, paint);

            canvas.drawPoint(10, 350, paint);
            canvas.drawPoints(new float[]{10, 360, 20, 360, 30, 360, 40, 360}, paint);

            RectF rectf3 = new RectF(10,370,110,420);
            canvas.drawRoundRect(rectf3, 6, 6, paint);


            Paint p = new Paint();
            p.setAntiAlias(true);
            p.setStrokeWidth(2);
            p.setStyle(Paint.Style.STROKE);

            p.setColor(Color.RED);
            canvas.drawCircle(50, 500, 30, p);

            p.setColor(Color.BLUE);
            canvas.drawCircle(100, 500, 30, p);

            p.setColor(Color.BLACK);
            canvas.drawCircle(150, 500, 30, p);

            p.setColor(Color.YELLOW);
            canvas.drawCircle(75, 550, 30, p);

            p.setColor(Color.GREEN);
            canvas.drawCircle(130, 550, 30, p);

        }

        //Draw Text
        if(buttonType == 3){
            Paint p = new Paint();
            p.setTextSize(20);
            canvas.drawText("欢迎来到我的画布",105,50,p);

            float[] pos = new float[]{10,60, 30,60, 50,60, 10,80, 30,80};
            canvas.drawPosText("很高兴见到",pos,p);
        }

        //Draw 2D
        if(buttonType == 4 ){
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.GREEN);
            paint.setStyle(Paint.Style.STROKE);
            paint.setTextSize(80);
            paint.setStrokeWidth(3);

            Path pCircle = new Path();
            pCircle.addCircle(50,50,30, Path.Direction.CCW);
            canvas.drawPath(pCircle, paint);

            Path pLine = new Path();
            pLine.moveTo(80, 50);
            pLine.lineTo(160, 70);
            pLine.lineTo(160, 20);
            pLine.lineTo(200, 60);
            pLine.close();
            canvas.drawPath(pLine, paint);

            Path pTr = new Path();
            pTr.moveTo(30, 100);
            pTr.lineTo(90, 140);
            pTr.lineTo(130, 100);
            pTr.close();
            canvas.drawPath(pTr, paint);


            paint.setStyle(Paint.Style.FILL);
            String str= "我爱安卓，Android爱我，我们是亲爱的一家。";
            Path pText = new Path();
            pText.addCircle(500, 600, 350, Path.Direction.CW);
            canvas.drawTextOnPath(str,pText,0,-18,paint);

        }

        //Draw Bitmap
        if(buttonType==5){
            Paint paint = new Paint();
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(R.drawable.image3);
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap bitmap = bitmapDrawable.getBitmap();
            canvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.drawBitmap(bitmap, 50, bitmap.getHeight(), paint);
            Log.d("med",bitmap.getHeight() + "--------------------------------------\n\n\n\n\n\n\n");
            Rect src = new Rect(300,300,500,500);
            Rect desc = new Rect(0,bitmap.getHeight()*2,200,bitmap.getHeight()*2+200);
            canvas.drawBitmap(bitmap, src, desc, paint);

            Rect src1 = new Rect(0,0,200,200);
            Rect desc1 = new Rect(0+200,bitmap.getHeight()*2,200+200,bitmap.getHeight()*2+200);
            canvas.drawBitmap(bitmap, src1, desc1, paint);
        }

        //Draw Android
        if(buttonType == 6){
            Paint paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setAntiAlias(true);

            //Draw Head
            RectF rectf = new RectF(150,300,450,600);
            canvas.drawArc(rectf, 0, -180, true, paint);
            //Draw Eye
            paint.setColor(Color.WHITE);
            canvas.drawCircle(225,375,8,paint);
            canvas.drawCircle(375, 375, 8, paint);

            paint.setColor(Color.GREEN);
            //Draw Line
            paint.setStrokeWidth(8);
            canvas.drawLine(245,310,210,190,paint);
            canvas.drawLine(355,310,385,190,paint);

            //Draw Body
            RectF rect1 = new RectF(150,480,450,780);
            canvas.drawRoundRect(rect1, 20, 20, paint);

            //Draw Arm
            RectF rect2 = new RectF(60,480,120,720);
            canvas.drawRoundRect(rect2, 20, 20, paint);
            rect2.offset(420, 0);
            canvas.drawRoundRect(rect2, 20, 20, paint);

            //Draw Leg
            RectF rect3 = new RectF(195,780,270,930);
            canvas.drawRoundRect(rect3,20,20,paint);
            rect3.offset(150,0);
            canvas.drawRoundRect(rect3,20,20,paint);
        }

        if(buttonType == 7){
            Paint paint = new Paint();
          //  Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.image3);
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(R.drawable.image3);
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            Bitmap bitmap = bitmapDrawable.getBitmap();
            Matrix matrix = new Matrix();
            matrix.setRotate(90, 200, 200);
            canvas.drawBitmap(bitmap,matrix,paint);

            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            //move bitmap
            matrix.setTranslate(width, 0);
            canvas.drawBitmap(bitmap, matrix, paint);

            matrix.postScale(0.6f, 0.6f);
            matrix.postRotate(-45);
            Bitmap bitmap3 = Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
            canvas.drawBitmap(bitmap3,0,height,paint);
        }

        //Test draw picture rectange
        if(buttonType == 8){
            Paint p = new Paint();
            p.setAntiAlias(true);
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(R.drawable.image3);
            Bitmap bitmap =( (BitmapDrawable)imageView.getDrawable()).getBitmap();
            canvas.translate(80, 60);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.WHITE);
            p.setStrokeWidth(2);

            RectF rect1 = new RectF(0,0,320,320);
            canvas.drawRoundRect(rect1, 10, 10, p);

            Matrix matrix = new Matrix();
            matrix.setScale(0.8f,0.8f);

            BitmapShader shader = new BitmapShader(Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true), Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
            p.setShader(shader);
            p.setShader(shader);
            canvas.drawRoundRect(rect1, 60, 60, p);
        }


    }
}
